package com.kk.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wk
 * @date 2025/2/27 22:27
 * @describe
 */
public class KKService extends Service {


    private static final String CHANNEL_ID = "music_channel";
    private static final int NOTIFICATION_ID = 1;
    private List<String> mMessageList = new ArrayList<>();

    private IBinder mIBinder = new IKKTestInterface.Stub() {
        @Override
        public void addMessage(String msg) throws RemoteException {

            KKService.this.addMessage(msg);
        }

        @Override
        public int getMessageSize() throws RemoteException {
            return KKService.this.getMessageSize();
        }

        @Override
        public String getMessage() throws RemoteException {
            return KKService.this.getMessage();
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mIBinder;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化通知渠道（Android 8.0+ 必须）
        createNotificationChannel();

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("My Foreground Service")
                .setContentText("Service is running in foreground")
                .build();

        startForeground(NOTIFICATION_ID, notification); // 开
    }


    private void addMessage(String msg) {
        synchronized (mMessageList) {
            mMessageList.add(msg);
        }
    }

    private int getMessageSize() {
        synchronized (mMessageList) {
            return mMessageList.size();
        }
    }

    private String getMessage() {
        synchronized (mMessageList) {
            if (mMessageList.isEmpty())
                return "";
            return mMessageList.remove(0);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "Music Player",
                    NotificationManager.IMPORTANCE_LOW
            );
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    private NotificationCompat.Builder buildNotification() {
        return new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Music Player")
                .setContentText("Playing music...");
    }
}
